﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BaW
{
    public partial class BaWScreen : Form
    {
        private int myStartX = 0;
        private int myStartY = 0;
        private int myStartYLabel = 0;
        private Font myLabelFont = new Font("Times New Roman", 25.0f);
        private Graphics myGraphics = null;
        private String myDisplayedTextCache = String.Empty;
        private const int WIDTH_SAFETY = 10;
        private const int HEIGH_SAFETY = 1;

        public Font TextFont
        {
            get { return myLabelFont; }
            set
            {
                myLabelFont = value;
                ShowText(myDisplayedTextCache);
            }
        }

        public int StartX
        {
            get { return myStartX; }
            set 
            { 
                myStartX = value;
                ShowText(myDisplayedTextCache);
            }
        }

        public int StartY
        {
            get { return myStartY; }
            set
            {
                myStartY = value;
                ShowText(myDisplayedTextCache);
            }
        }


        public BaWScreen()
        {
            InitializeComponent();

            myGraphics = this.CreateGraphics();
        }

        public void ShowText(String theTextToShow)
        {
            ClearText();

            if (null == theTextToShow || String.Empty == theTextToShow)
            {                
                return;
            }
            myDisplayedTextCache = theTextToShow;
                        
            StringReader aStringReader = new StringReader(theTextToShow);

            string aLine = aStringReader.ReadLine();
            
            while (null != aLine && String.Empty != aLine)
            {                
                Label aNewLine = new Label();
                aNewLine.Text = ParseText(aLine);
                aNewLine.Font = myLabelFont;                

                SizeF aTextSize = GetTextLength(aLine);
                aNewLine.Width = (int)Math.Round(aTextSize.Width, 0, MidpointRounding.AwayFromZero) + WIDTH_SAFETY;
                aNewLine.Height = (int)Math.Round(aTextSize.Height, 0, MidpointRounding.AwayFromZero) + HEIGH_SAFETY;
                aNewLine.Location = new Point(myStartX, myStartY + myStartYLabel);

                aNewLine.Enabled = true;
                aNewLine.Visible = true;
                this.Controls.Add(aNewLine);

                myStartYLabel += aNewLine.Height;

                aLine = aStringReader.ReadLine();
            }
        }

        private SizeF GetTextLength(string theText)
        {
            if (null == myGraphics)
            {
                myGraphics = this.CreateGraphics();
            }

            if (null == theText || String.Empty == theText)
            {
                return new SizeF(0, 0);
            }

            return myGraphics.MeasureString(theText, myLabelFont);            
        }

        /// <summary>
        /// Method replaces some special characters in the text given as a parameter
        /// </summary>
        /// <param name="theText"></param>
        /// <returns></returns>
        private string ParseText(string theText)
        {
            return theText.Replace("\t", "                ");
        }

        private void ClearText()
        {
            myStartYLabel = 0;            
            myDisplayedTextCache = String.Empty;

            IList<Label> aListOfLabels = new List<Label>();
            foreach (Control aControl in this.Controls)
            {
                aListOfLabels.Add((Label)aControl);
            }

            foreach (Label aLabel in aListOfLabels)
            {
                this.Controls.Remove(aLabel);
            }
        }
    }
}
